package com.billdowney.security.handler;

import java.io.IOException;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.stereotype.Service;

import com.billdowney.dao.user.UserLoginInfoDao;
import com.billdowney.entity.user.UserLoginInfo;
import com.billdowney.entity.user.UserLoginRecordStatus;
import com.billdowney.service.impl.base.BaseServiceImpl;
import com.billdowney.service.interfaces.user.UserLoginRecordService;
import com.billdowney.util.http.HttpIOUtil;
import com.billdowney.util.return_type.ReturnData;

/**
 * 登出成功处理
 * 
 * @author 超级小富翁 - BillDowney
 * @date 2019年2月11日 下午1:15:08
 */
@Service
public class LoginLogoutHandler extends BaseServiceImpl<UserLoginInfoDao, UserLoginInfo, String>
		implements LogoutSuccessHandler {

	@Autowired
	private UserLoginRecordService loginRecordService;

	@Override
	public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication)
			throws IOException, ServletException {
		try {
			UserLoginInfo userLoginInfo = (UserLoginInfo) authentication.getPrincipal();
			loginRecordService.addUserLoginRecord(request, userLoginInfo, UserLoginRecordStatus.userLoginOut);
		} catch (Exception e) {
			e.printStackTrace();
		}
		HttpIOUtil.outJsonData(response, new ReturnData());
	}

	@Resource(type = UserLoginInfoDao.class)
	@Override
	public void setDao(UserLoginInfoDao userLoginInfoDao) {
		super.dao = userLoginInfoDao;
	}

}
